﻿//by  张海军 710605420@qq.com
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System;
using System.Xml.Serialization;

namespace LazyFramework
{
    public class UIConfig : Singleton<UIConfig>
    {
        private Dictionary<UIType, Info> m_allUIConfig = new Dictionary<UIType, Info>();
        private UIInfo m_UIInfo;

        private void LoadXml<T>(string path, ref T ms) where T : class
        {
            try
            {
                using (TextReader reader = new StreamReader(path))
                {
                    var serializer = new XmlSerializer(typeof(T));
                    var item = (T)serializer.Deserialize(reader);
                    if (item != null)
                    {
                        ms = item;
                    }
                }
            }
            catch (Exception ex)
            {

                Debug.LogError(ex.Message);
            }
        }

        [XmlRoot("UIInfo")]
        public class UIInfo
        {
            [XmlElement("UI")]
            public Info[] Ins { set; get; }
        }
        public class Info
        {
            [XmlAttribute(AttributeName = "uitype")]
            public string uitype { set; get; }
            [XmlAttribute(AttributeName = "prefabname")]
            public string prefabname { set; get; }
            [XmlAttribute(AttributeName = "classname")]
            public string classname { set; get; }
            [XmlAttribute(AttributeName = "isLuauI")]
            public string isLuauI { set; get; }
        }
        public void Load()
        {
            string path = Application.streamingAssetsPath + "/Config/UIConfig.xml";
            LoadXml(path, ref m_UIInfo);
            for (int i = 0; i < m_UIInfo.Ins.Length; ++i)
            {
                UIType uiType = (UIType)Enum.Parse(typeof(UIType), m_UIInfo.Ins[i].uitype);
                m_allUIConfig[uiType] = m_UIInfo.Ins[i];
            }
        }

        public Info GetUIConfig(UIType uiType)
        {
            if(m_allUIConfig.ContainsKey(uiType))
            {
                return m_allUIConfig[uiType];
            }
            return default(Info);
        }
    }

}
